# 读取单个txt文件
import os
from langchain_community.document_loaders import TextLoader
# 获取当前脚本文件所在的目录
script_dir = os.path.dirname(__file__)
print(f"获取当前脚本文件所在的目录：{script_dir}")
# 结合相对路径构建完整路径
file_dir = os.path.join(script_dir, 'D:/ideaSpace/rag-in-action-master/90-文档-Data/黑悟空/设定.txt')
if not os.path.exists(file_dir):
    print(f"文件不存在: {file_dir}")
    exit(1)

loader = TextLoader(file_dir, encoding='utf-8')
documents = loader.load()
print(documents)

# 构建Document对象
from langchain_core.documents import Document
documents = [
    Document(
        page_content="悟空是大师兄.",
        metadata={"source": "师徒四人.txt"},
    ),
    Document(
        page_content="八戒是二师兄.",
        metadata={"source": "师徒四人.txt "},
    ),
]
print(documents)

